const Router = require("koa-router");
const Mock = require("mockjs")
const router = new Router({
    prefix: "/api"
});

router.get("/", async function (ctx) {
    ctx.body = "服务器运行正常"
});

router.post("/files", async function (ctx) {
    // ctx.request.body // 加载有 koa-body 插件根据请求参数类型解析后的参数
    let result = {
        state: true,
    }
    if (ctx.request.files && ctx.request.files.img) {
        result.path = {
            img: "http://127.0.0.1:3000/upload/" + ctx.request.files.img.newFilename
        }
    }
    ctx.body = result
})
router.get("/search", async function (ctx) {
    let wk = ctx.query.wk
    ctx.body = Mock.mock({
        state: true,
        "list|10": [
            wk + "@cword(5,5)"
        ]
    })
})

router.get("/word", async function (ctx) {
    ctx.body = Mock.mock("@cword(5,5)")
})

router.get("/checkUsername", async function (ctx) {
    let username = ctx.query.username
    let usernames = ["admin", "tom", "jack"];
    if (usernames.includes(username)) {
        ctx.body = {
            state: false,
            msg: "用户名已存在"
        }
    } else {
        ctx.body = {
            state: true,
            msg: "用户名可用"
        }
    }
})

// var option = {
//     title: {
//       text: 'ECharts 入门示例'
//     },
//     tooltip: {},
//     legend: {
//       data: ['销量']
//     },
//     xAxis: {
//       data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
//     },
//     yAxis: {},
//     series: [
//       {
//         name: '销量',
//         type: 'bar',
//         data: [5, 20, 36, 10, 10, 20]
//       }
//     ]
//   };
router.get("/getSales", async function (ctx) {
    let date = ctx.query.date||"2022-12-22";
    ctx.body = Mock.mock({
        title: {
            text: date+"销售统计"
        },
        tooltip: {},
        legend: {
            data: ['销量']
        },
        xAxis: {
            data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
            {
                name: '销量',
                type: 'bar',
                "data|6": ["@integer(10,100)"]
            }
        ]
    })
})
router.get("/users",async function(ctx){
    let objs = {
        "users|4": [
            {
                no: "@id",
                name: "@cname",
                "sex|1": ["男", "女", "未知"],
                tel: /^1[0-9]{10}$/,
                img: "https://api.ixiaowai.cn/mcapi/mcapi.php?_=@datetime(T)"
            }
        ]
    };
    ctx.body = Mock.mock(objs).users;
})
module.exports = router;